package com.ithao.service.impl;

import com.ithao.dao.TagDao;
import com.ithao.database.MmDaoException;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.pojo.Tag;
import com.ithao.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description ：学科标签业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
@Transactional
public class TagServiceImpl implements TagService {
    @Autowired
    private TagDao tagDao;
    @Override
    public void addTag(Tag tag) {
        log.info("addTag:{}", tag);
        Integer result = tagDao.addTag(tag);
        if (result == 0) {
            throw new MmDaoException("添加标签失败");
        }
    }

    @Override
    public PageResult findListByPage(QueryPageBean queryPage) {
        // 获取分页数据集
        List<Tag> tagList = tagDao.selectListByPage(queryPage);
        // 获取基于条件的总记录数
        Long totalCount = tagDao.selectTotalCount(queryPage);
        return new PageResult(totalCount, tagList);
    }

    @Override
    public void deleteTag(Integer id) {
        log.info("addTag,id:{}", id);
            Integer result = tagDao.deleteTag(id);
            if (result == 0) {
                throw new RuntimeException("删除标签失败");
            }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        int result = tagDao.updateStatus(id, status);
        if (result == 0) {
            throw new RuntimeException("更改标签状态失败");
        }
    }

    @Override
    public void update(Tag tag) {
        tagDao.update(tag);
    }
}
